home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLSRC.PAK / CHECKLST.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  7.0 KB  |  349 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.8  $
  6. //
  7. // Implements TCheckList and TCheckListItem
  8. //----------------------------------------------------------------------------
  9. #include <owl/pch.h>
  10. #include <owl/checklst.h>
  11. #include <owl/commctrl.h>
  12. #include <owl/uihelper.h>
  13. #include <string.h>
  14.  
  15. OWL_DIAGINFO;
  16. DIAG_DECLARE_GROUP(OwlControl);
  17.  
  18. const int CheckBoxWidth = 15;
  19.  
  20. //
  21. // Constructor to create a window.
  22. //
  23. TCheckList::TCheckList(TWindow* parent, int id, int x, int y, int w, int h,
  24.                        TCheckListItem* items, int numItems,
  25.                        TModule* module)
  26. :
  27.   TListBox(parent, id, x, y, w, h, module),
  28.   Items(items),
  29.   NumberOfItems(numItems)
  30. {
  31.   Attr.Style |= LBS_NOTIFY | LBS_OWNERDRAWVARIABLE | LBS_NOINTEGRALHEIGHT;
  32.   Attr.Style &= ~LBS_HASSTRINGS;
  33.   Attr.Style &= ~LBS_SORT;
  34.   TRACEX(OwlControl, OWL_CDLEVEL, "TCheckList constructed @" << (void*)this);
  35. }
  36.  
  37. //
  38. // Constructor used for resources
  39. //
  40. TCheckList::TCheckList(TWindow* parent, int resourceId,
  41.                        TCheckListItem* items, int numItems,
  42.                        TModule* module)
  43. :
  44.   TListBox(parent, resourceId, module),
  45.   Items(items),
  46.   NumberOfItems(numItems)
  47. {
  48.   TRACEX(OwlControl, OWL_CDLEVEL, "TCheckList constructed from resource @" << (void*)this);
  49. }
  50.  
  51. //
  52. //
  53. //
  54. TCheckList::~TCheckList()
  55. {
  56.   TRACEX(OwlControl, OWL_CDLEVEL, "TCheckList destructed @" << (void*)this);
  57. }
  58.  
  59. DEFINE_RESPONSE_TABLE1(TCheckList, TListBox)
  60.   EV_WM_LBUTTONDOWN,
  61.   EV_WM_CHAR,
  62. END_RESPONSE_TABLE;
  63.  
  64. //
  65. // Toggle the "checked" state when the space is pressed.
  66. //
  67. void
  68. TCheckList::EvChar(uint key, uint repeatCount, uint flags)
  69. {
  70.   TCheckListItem* item = GetItemAtIndex(GetCaretIndex());
  71.  
  72.   if (item) {
  73.     if (key == ' ') {
  74.       item->Toggle();
  75.       Update();
  76.     }
  77.   }
  78.   TListBox::EvChar(key, repeatCount, flags);
  79. }
  80.  
  81. //
  82. // Toggle the "checked" state when the mouse is clicked in the checkbox.
  83. //
  84. void
  85. TCheckList::EvLButtonDown(uint modKeys, TPoint& point)
  86. {
  87.   TListBox::EvLButtonDown(modKeys, point);
  88.   TCheckListItem* item = GetItemAtIndex(GetCaretIndex());
  89.   if (point.x < CheckBoxWidth) {
  90.     item->Toggle();
  91.     Update();
  92.   }
  93. }
  94.  
  95. //
  96. // Add the strings into the listbox.
  97. //
  98. void
  99. TCheckList::SetupWindow()
  100. {
  101.   TRACEX(OwlControl, 1, "TCheckList::SetupWindow() @" << (void*)this);
  102.  
  103.   // Call the base class
  104.   //
  105.   TListBox::SetupWindow();
  106.   for (int i = 0; i < NumberOfItems; i++)
  107.     AddString((char far*) &Items[i]);
  108. }
  109.  
  110. //
  111. // Refresh the window.
  112. //
  113. void
  114. TCheckList::Update()
  115. {
  116.   int topIndex = GetTopIndex();
  117.   int selIndex = GetSelIndex();
  118.   SendMessage(WM_SETREDRAW, false);
  119.   Invalidate();
  120.   SetTopIndex(topIndex);
  121.   if (selIndex != LB_ERR)
  122.     SetSelIndex(selIndex);
  123.   SendMessage(WM_SETREDRAW, true);
  124. }
  125.  
  126. //
  127. // Repaint the item entirely.
  128. //
  129. void
  130. TCheckList::ODAFocus(DRAWITEMSTRUCT far& drawInfo)
  131. {
  132.   PaintItem(drawInfo);
  133. }
  134.  
  135. //
  136. // Repaint the item entirely.
  137. //
  138. void
  139. TCheckList::ODASelect(DRAWITEMSTRUCT far& drawInfo)
  140. {
  141.   PaintItem(drawInfo);
  142. }
  143.  
  144. //
  145. // Repaint the item entirely.
  146. //
  147. void
  148. TCheckList::ODADrawEntire(DRAWITEMSTRUCT far& drawInfo)
  149. {
  150.   PaintItem(drawInfo);
  151. }
  152.  
  153. //
  154. // Paint the item entirely.
  155. //
  156. void
  157. TCheckList::PaintItem(DRAWITEMSTRUCT far& drawInfo)
  158. {
  159.   TCheckListItem* item = GetItemAtIndex(drawInfo.itemID);
  160.   if (item == 0)
  161.     return;
  162.  
  163.   // Prepare DC
  164.   //
  165.   TDC dc(drawInfo.hDC);
  166.  
  167.   // Erase entire line
  168.   //
  169.   TRect rect(drawInfo.rcItem);
  170.   TBrush bkgnd(TColor::SysWindow);
  171.   dc.FillRect(rect, bkgnd);
  172.  
  173.   // Draw checkbox
  174.   //
  175.   TRect checkboxRect(rect.left + 1, rect.top + 1, rect.left + CheckBoxWidth, rect.bottom - 1);
  176.   dc.Rectangle(checkboxRect);
  177.  
  178.   if (item->IsIndeterminate()) {
  179.     TRect fillRect = checkboxRect;
  180.     fillRect.Inflate(-1, -1);
  181.     TBrush fillBrush(TColor::Sys3dFace);
  182.     dc.FillRect(fillRect, fillBrush);
  183.   }
  184.  
  185.   // Draw check if needed
  186.   //
  187.   if (item->IsChecked() || item->IsIndeterminate()) {
  188.     int cx = (checkboxRect.right - checkboxRect.left) / 2 + checkboxRect.left ;
  189.     int cy = (checkboxRect.bottom - checkboxRect.top) / 2 + checkboxRect.top + 2;
  190.  
  191.     TPen checkPen(TColor::Black, 2);
  192.     dc.SelectObject(checkPen);
  193.  
  194.     dc.MoveTo(cx, cy);
  195.     dc.LineTo(cx - 3, cy - 3);
  196.     dc.MoveTo(cx, cy);
  197.     dc.LineTo(checkboxRect.right - 2, checkboxRect.top + 2);
  198.  
  199.     dc.RestorePen();
  200.   }
  201.  
  202.   // Draw Text
  203.   //
  204.   TRect textRect = rect;
  205.   textRect.left = checkboxRect.right + 2;
  206.  
  207.   dc.DrawText(item->Text, -1, textRect, DT_SINGLELINE | DT_VCENTER);
  208.  
  209.   // Draw focus and selected states
  210.   //
  211.   if (drawInfo.itemState & ODS_FOCUS)
  212.     dc.DrawFocusRect(textRect);
  213.  
  214.   if (drawInfo.itemState & ODS_SELECTED)
  215.     dc.InvertRect(textRect);
  216. }
  217.  
  218. //
  219. // Return the item at the specified index.
  220. //
  221. TCheckListItem*
  222. TCheckList::GetItemAtIndex(int index)
  223. {
  224.   if (index < 0 || index >= GetCount())
  225.     return 0;
  226.   return (TCheckListItem*)GetItemData(index);
  227. }
  228.  
  229.  
  230. //----------------------------------------------------------------------------
  231. // TCheckListItem
  232. //
  233.  
  234. //
  235. // Initialize the state of TCheckListItem.
  236. //
  237. TCheckListItem::TCheckListItem()
  238. :
  239.   State(BF_UNCHECKED),
  240.   HasThreeStates(false),
  241.   Text(0)
  242. {
  243. }
  244.  
  245. //
  246. // Construct the object with a text string and a starting state.
  247. //
  248. TCheckListItem::TCheckListItem(const char far* text, uint state)
  249. :
  250.   HasThreeStates(ToBool(state == BF_GRAYED)),
  251.   State(state)
  252. {
  253.   SetText(text);
  254. }
  255.  
  256. //
  257. // Delete the allocated copied text.
  258. //
  259. TCheckListItem::~TCheckListItem()
  260. {
  261.   delete[] Text;
  262. }
  263.  
  264. //
  265. // Toggle the state of the item.
  266. // If the item has three states, the cycle goes from
  267. //   unchecked -> checked -> indeterminate -> back to unchecked.
  268. // Otherwise the state toggles between
  269. //   unchecked and checked.
  270. //
  271. void
  272. TCheckListItem::Toggle()
  273. {
  274.   if (HasThreeStates) {
  275.     if (IsIndeterminate())
  276.       Uncheck();
  277.     else if (IsChecked())
  278.       SetIndeterminate();
  279.     else
  280.       Check();
  281.   }
  282.   else {
  283.    if (IsChecked())
  284.      Uncheck();
  285.    else
  286.      Check();
  287.   }
  288. }
  289.  
  290. //
  291. // Programmatically check the item.
  292. //
  293. void
  294. TCheckListItem::Check()
  295. {
  296.   State = BF_CHECKED;
  297. }
  298.  
  299. //
  300. // Programmatically uncheck the item.
  301. //
  302. void
  303. TCheckListItem::Uncheck()
  304. {
  305.   State = BF_UNCHECKED;
  306. }
  307.  
  308. //
  309. // Programmatically make the item indeterminate.
  310. //
  311. void
  312. TCheckListItem::SetIndeterminate()
  313. {
  314.   State = BF_GRAYED;
  315.   HasThreeStates = true;
  316. }
  317.  
  318. //
  319. // Set the three-state property.
  320. //
  321. void
  322. TCheckListItem::SetThreeStates(bool hasThreeStates)
  323. {
  324.   HasThreeStates = hasThreeStates;
  325.   if (IsIndeterminate() && !hasThreeStates)
  326.     Check();
  327. }
  328.  
  329. //
  330. // Copy the text string.
  331. //
  332. void
  333. TCheckListItem::SetText(const char far* text)
  334. {
  335.   delete[] Text;
  336.   Text = strnewdup(text);
  337. }
  338.  
  339. //
  340. // Return the text of the item.
  341. //
  342. void
  343. TCheckListItem::GetText(char far* buffer, int len)
  344. {
  345.   strncpy(buffer, Text, len);
  346. }
  347.  
  348.  
  349.